package com.crm.system.service.impl.common;

import com.crm.common.model.Enum.DictionaryEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.common.Dictionary;
import com.crm.service.common.DictionaryDbService;
import com.crm.system.service.common.DictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2021/1/20 8:41
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryDbService dictionaryDbService;

    /**
     * 根据字典code(type_code)查询字典信息
     * @param typeCode
     * @return
     */
    @Override
    public List<Dictionary> selectDictionaryByTypeCode(String typeCode) {
        List<Dictionary> dictionaryList = dictionaryDbService.selectDictionaryByTypeCode(typeCode);
        return dictionaryList;
    }

    /**
     * 添加字典信息
     * @param dictionary
     * @return
     */
    @Override
    public Boolean insertDictionary(Dictionary dictionary) {
        dictionary.setDictId(CommonUtils.getGUID());//主键
        dictionary.setCreateTm(new Date());//创建时间
        String dictCode = dictionaryDbService.selectDictCode(dictionary.getTypeCode());
        if(!StringUtils.isBlank(dictCode)){
            dictionary.setDictCode(Integer.valueOf(dictCode) + 1 +"");//字典小分类
        }
        return dictionaryDbService.insertDictionary(dictionary);
    }

    /**
     * 根据大分类、小分类确定唯一一条字典数据
     * @param typeCode
     * @param dictCode
     * @return
     */
    @Override
    public Dictionary selectDictionaryByOne(String typeCode, String dictCode) {
        return dictionaryDbService.selectDictionaryByOne(typeCode,dictCode);
    }
}
